--
-- Author: shunguo.chen
-- Date: 2018-07-27 15:50:36
--
-- LifePanel 生命数panel
-- 

local LifeRender = import(".renders.LifeRender")

local ClassRef = app.mvc.createPanelClass(...)

function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node)
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

-- data = {leftReviveTimes=, rightReviveTimes=, leftKillTimes=, rightKillTimes=}
function ClassRef:init(data)
    self.super.init(self, data)

    self._leftKillNum = self._ui["Panel/num_1"]
    self._rightKillNum = self._ui["Panel/num_2"]

    -- self:refresh()
end

function ClassRef:_onActionOver(render)
	if self._actionOverCB then
		self._actionOverCB()
		self._actionOverCB = nil
	end
end

function ClassRef:onActionOver(cb)
	self._actionOverCB = cb
end

-- tp = 1 竞技场
-- tp = 2 歌剧院pve
function ClassRef:initType(tp)
	self._type = tp
	local maxNum = 5
	if tp == 1 then
		maxNum = 5
		self._ui.img_vs:setVisible(false)
		self._leftKillNum:setVisible(true)
		self._rightKillNum:setVisible(true)
	elseif tp == 2 then
		maxNum = 10
		self._ui.img_vs:setVisible(true)
		self._leftKillNum:setVisible(false)
		self._rightKillNum:setVisible(false)
	end

	local ui = self._ui
	local leftRenders = {0,0,0,0,0}
	local rightRenders = {0,0,0,0,0}
	local render
	for i=1,maxNum do
		render = LifeRender.create(ui["Panel/b_" .. i])
		render:setIsEnemy(false)
		render:setVisible(true)
		render:onActionOver(handler(self, self._onActionOver))
		leftRenders[i] = render

		render = LifeRender.create(ui["Panel/r_" .. i])
		render:setIsEnemy(true)
		render:setVisible(true)
		render:onActionOver(handler(self, self._onActionOver))
		rightRenders[i] = render
	end
	self._leftRenders = leftRenders
	self._rightRenders = rightRenders
end

function ClassRef:refresh()
    if self._data then
        local data = self._data
        local leftReviveTimes = data.leftReviveTimes
        local rightReviveTimes = data.rightReviveTimes

        if self._type == 1 then
        	self._leftKillNum:setString(data.leftKillTimes)
        	self._rightKillNum:setString(data.rightKillTimes)
        end

        local lenth = #self._leftRenders

        -- 左侧剩余生命数
        for i = 1, lenth do
            self._leftRenders[i]:setData( i <= leftReviveTimes )
        end

        -- 右侧剩余生命数
        for i = 1, lenth do
            self._rightRenders[i]:setData( i <= rightReviveTimes )
        end
    end
end

return ClassRef